package com.TetrisII.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

import com.TetrisII.global.Global;
import com.TetrisII.views.GamePanel;


/**
 * 底层面板类
 * 
 * @author Administrator
 *
 */

public class Ground {
	private Shape shape;
	private int goals;

	// 这个面板接收图形，表示Global.WID列，Global.HGH+4行
	private int[][] ground = new int[Global.WID][Global.HGH + 4];

	/**
	 * 构造方法初始化
	 */
	public Ground() {
		initGround();
	}

	/**
	 * 初始化障碍物面板
	 * 
	 * @param ground
	 */
	public void initGround() {
		for (int i = 0; i < ground.length; i++) {
			for (int j = 0; j < ground[i].length; j++) {
				ground[i][j] = 0;

			}
		}
		goals = 0;
	}

	/**
	 * 消行
	 * 
	 * @throws InterruptedException
	 */
	public void delectLine() {

		for (int j = Global.HGH + 3; j > 0; j--) {

			if (isDelect(j)) {

				for (int k = j; k > 0; k--) {

					for (int i = 0; i < Global.WID; i++) {
						ground[i][k] = ground[i][k - 1];
					}
				}
				goals = goals + 10;
				j++;// 连续多行删除
			}

		}
	}

	/**
	 * 判断游戏是否结束
	 * 
	 * @return
	 */
	public boolean gameOver() {
		boolean over = false;
		for (int i = 0; i < ground.length; i++) {
			if (ground[i][4] == 1)
				over = true;
		}
		return over;
	}

	/**
	 * 判断哪行需要消行
	 * 
	 * @return
	 */
	public boolean isDelect(int line) {
		boolean delect = false;
		int cutLine = 0;

		for (int i = 0; i < Global.WID; i++) {
			if (ground[i][line] == 1)
				cutLine++;
			if (cutLine == Global.WID) {
				delect = true;
			}
		}

		return delect;
	}

	/**
	 * 画出障碍物面板
	 */
	public void paintGround(Graphics g,GamePanel panel) {
		Graphics2D g_2 = (Graphics2D) g;
		URL url=GamePanel.class.getResource("方块.PNG");
		Image image=Toolkit.getDefaultToolkit().getImage(url);
		for (int i = 0; i < ground.length; i++) {
			for (int j = 4; j < ground[i].length; j++) {

				if (ground[i][j] == 1) {
					g_2.drawImage(image, i * Global.CELL, (j - 4) * Global.CELL, Global.CELL, Global.CELL,panel);
				}
			}

		}
	}

	/**
	 * 接收图形并把图形变成障碍物
	 */
	public void accept(Shape shape) {
		int[][] shapes = shape.getShapes();
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (shape.isPaint(i, j)) {
					ground[shape.getX() + i][shape.getY() + j + 4] = 1;

				}
			}

		}

		shape.initShape();// 接收完后，初始化方块
	}

	/**
	 * 底层上涨
	 */
	public void upToGround() {

		for (int j = 0; j < Global.HGH + 3; j++) {
			for (int i = 0; i < Global.WID; i++) {
				ground[i][j] = ground[i][j + 1];
			}
		}
		for (int k = 0; k < Global.WID; k++) {
			int space = (int) (Math.random() * 2);
			ground[k][Global.HGH + 3] = space;
		}
	}

	/**
	 * 炸弹，炸毁四行，并且自己消失
	 */
	public void bomb() {
		for (int k = 0; k < 4; k++) {
			for (int j = Global.HGH + 3; j > 0; j--) {
				for (int i = 0; i < Global.WID; i++) {
					ground[i][j] = ground[i][j - 1];
				}
			}

		}
	}

	/**
	 * 是否可移动,这个方法要加入线程锁，不然按住向下键会越出
	 * 
	 * @param shape   图形种类
	 *          
	 * @param direction 图形的移动方向
	 *           
	 * @return 能否朝direction的方向移动
	 */
	public  synchronized boolean isMoveable(Shape shape, DirectionEnum direction) {
		boolean move = false;
		int[][] shapes = shape.getShapes();
		int left = shape.getX();
		int top = shape.getY();
		int status = shape.getStatus();
		if (direction == DirectionEnum.DOWN)
			top++;
		if (direction == DirectionEnum.LEFT)
			left--;
		if (direction == DirectionEnum.RIGHT)
			left++;
		if (direction == DirectionEnum.CHANGE)
			status = (status + 1) % 4;

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (shapes[status][4 * j + i] == 1) {
					if (top + j >= Global.HGH || left + i < 0 || left + i >= Global.WID
							|| ground[i + left][top + j + 4] == 1) {
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * 返回面板数组
	 * 
	 * @return 面板数组
	 */
	public int[][] getGround() {
		return ground;
	}

	/**
	 * 设置面板数组
	 * 
	 * @param ground
	 */
	public void setGround(int[][] ground) {
		this.ground = ground;
	}

	/**
	 * 返回分数
	 * 
	 * @return
	 */
	public int getGoals() {
		return goals;
	}

}
